import {
	router,
	addRoutes
} from "~/router"
import {
	getToken
} from "~/composables/auth"
import {
	toast,
	showFullLoading,
	hideFullLoading
} from "~/composables/util"
import store from "./store"
let hasGetInfo = false
router.beforeEach(async (to, from, next) => {
	//显示loading
	showFullLoading()

	const token = getToken()

	//没有登录，强制跳转至登录页
	if (!token && to.path != "/login") {
		toast("请先登录", "error")
		return next({
			path: "/login"
		})
	}

	//防止重复登录
	if (token && to.path == "/login") {
		toast("请勿重复登录", "error")
		return next({
			path: from.path ? from.path : "/"
		})
	}

	let hasNewRoutes = false
	if (token && !hasGetInfo) {
		let {
			menus
		} = await store.dispatch("getinfo")
		hasGetInfo = true
		// 动态添加路由
		hasNewRoutes = addRoutes(menus)
	}

	//设置页面标题
	let title = (to.meta.title ? to.meta.title : "") + "-飞天商城后台"
	document.title = title

	hasNewRoutes ? next(to.fullPath) : next()

	if (token) {
		await store.dispatch("getinfo")
	}

})

//全局后置守卫
router.afterEach((to, from) => hideFullLoading())